package com.ruoyi.system.utils;

import com.ruoyi.system.domain.ResultVO;
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName: TokenUtils
 * @Description: 萤石云token工具类
 * @Author: liu
 * @Date: 2020/11/17 15:54
 */
public class TokenUtils {
    /**
     * 向目的URL发送post请求
     * @param url       目的url
     * @param params    发送的参数
     * @return  ResultVO
     */
    public static ResultVO sendPostRequest(String url, MultiValueMap<String, String> params){
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpMethod method = HttpMethod.POST;
        System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
        headers.add(HttpHeaders.CONTENT_TYPE,"application/x-www-form-urlencoded");
        headers.add(HttpHeaders.HOST,"open.ys7.com");
        //将请求头部和参数合成一个请求
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<ResultVO> response = client.exchange(url, method, requestEntity, ResultVO.class);
        return response.getBody();
    }
}
